class User < ApplicationRecord
  before_save {self.email.downcase!}
  validates :name, presence:true, length:{maximum:50}
  email_regex = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email, presence:true, length:{maximum:255}, format:{with:email_regex}, 
                    uniqueness:{case_sensitive:false}
  has_secure_password
  validates :password, presence:true, length:{minimum:6}

  # 返回字符串(密码)哈希摘要
  def User.digest(string)
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost
    BCrypt::Password.create(string,cost:cost)
  end
end
